<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      

      <el-form-item label="编码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="简称" prop="alternativeName">
        <el-input
          v-model="queryParams.alternativeName"
          placeholder="请输入简称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="查询码" prop="lookup">
        <el-input
          v-model="queryParams.lookup"
          placeholder="请输入查询码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入分类"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格" prop="specification">
        <el-input
          v-model="queryParams.specification"
          placeholder="请输入规格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="唯一标识" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入唯一标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商id" prop="vendorId">
        <el-input
          v-model="queryParams.vendorId"
          placeholder="请输入供应商id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上架标志" prop="published">
        <el-select v-model="queryParams.published" placeholder="请选择上架标志" clearable size="small">
          <el-option
            v-for="dict in publishedOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建者" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入创建者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable size="small"
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新者" prop="updatedBy">
        <el-input
          v-model="queryParams.updatedBy"
          placeholder="请输入更新者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker clearable size="small"
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="goAddPage"
          v-hasPermi="['catalog:product:add']"
        >添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['catalog:product:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['catalog:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['catalog:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['catalog:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品id" align="center" prop="productId" />
      <!-- <el-table-column label="名称" align="center" prop="productName" /> -->
      <el-table-column label="名称" >
        <template slot-scope="scope">
          <!-- <el-link type="primary" :href="'/catalog/product/' + scope.row.productId" 
          target="_blank"	class="buttonText">{{scope.row.productName}}</el-link> -->
          <router-link :to="'/catalog/product/' + scope.row.productId" class="el-link el-link--primary">
            <span>{{ scope.row.productName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="编码" align="center" prop="productCode" />
      <!-- <el-table-column label="简称" align="center" prop="alternativeName" />
      <el-table-column label="查询码" align="center" prop="lookup" /> -->
      <el-table-column label="分类" align="center" prop="categoryId" />
      <el-table-column label="封面图片" align="center" prop="coverId" />
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="采购单位" align="center" prop="purchaseUnit" />
      <el-table-column label="单位" align="center" prop="standardUnit" />
      <el-table-column label="拆零单位" align="center" prop="unboxUnit" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <!-- <el-table-column label="seo关键字" align="center" prop="metaKeywords" />
      <el-table-column label="seo标题" align="center" prop="metaTitle" />
      <el-table-column label="seo描述" align="center" prop="metaDescription" /> -->
      <el-table-column label="唯一标识" align="center" prop="sku" />
      <!-- <el-table-column label="商品简述" align="center" prop="shortDescription" />
      <el-table-column label="商品详情" align="center" prop="fullDescription" /> -->
      <el-table-column label="供应商id" align="center" prop="vendorId" />
      <el-table-column label="售价" align="center" prop="salesPrice" />
      <el-table-column label="原价" align="center" prop="oldSalesPrice" />
      <el-table-column label="采购价" align="center" prop="purchasePrice" />
      <el-table-column label="上架标志" align="center" prop="published" :formatter="publishedFormat" />
      <el-table-column label="删除标志" align="center" prop="deleted" :formatter="deletedFormat" />
      <el-table-column label="显示顺序" align="center" prop="displayOrder" />
      <el-table-column label="创建者" align="center" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="更新者" align="center" prop="updatedBy" />
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['catalog:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['catalog:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>

      

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

<el-row>
  <el-col :span="12">
    <div class="grid-content ">


<el-form-item label="名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="编码" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="简称" prop="alternativeName">
          <el-input v-model="form.alternativeName" placeholder="请输入简称" />
        </el-form-item>
        <el-form-item label="查询码" prop="lookup">
          <el-input v-model="form.lookup" placeholder="请输入查询码" />
        </el-form-item>
        <el-form-item label="分类" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入分类" />
        </el-form-item>
        <el-form-item label="封面图片">
          <imageUpload v-model="form.coverUrl" :pictureType="1" v-on:uploaded="onCoverUploaded"/>
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="采购单位" prop="purchaseUnit">
          <el-input v-model="form.purchaseUnit" placeholder="请输入采购单位" />
        </el-form-item>
        <el-form-item label="单位" prop="standardUnit">
          <el-input v-model="form.standardUnit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="拆零单位" prop="unboxUnit">
          <el-input v-model="form.unboxUnit" placeholder="请输入拆零单位" />
        </el-form-item>
         <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
         <el-form-item label="唯一标识" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入唯一标识" />
        </el-form-item>


    </div>
    </el-col>
  <el-col :span="12">
    <div class="grid-content ">


       
        <el-form-item label="seo关键字" prop="metaKeywords">
          <el-input v-model="form.metaKeywords" placeholder="请输入seo关键字" />
        </el-form-item>
        <el-form-item label="seo标题" prop="metaTitle">
          <el-input v-model="form.metaTitle" placeholder="请输入seo标题" />
        </el-form-item>
        <el-form-item label="seo描述" prop="metaDescription">
          <el-input v-model="form.metaDescription" placeholder="请输入seo描述" />
        </el-form-item>
       
        <el-form-item label="商品简述" prop="shortDescription">
          <el-input v-model="form.shortDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品详情" prop="fullDescription">
          <el-input v-model="form.fullDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="供应商id" prop="vendorId">
          <el-input v-model="form.vendorId" placeholder="请输入供应商id" />
        </el-form-item>
        <el-form-item label="售价" prop="salesPrice">
          <el-input v-model="form.salesPrice" placeholder="请输入售价" />
        </el-form-item>
        <el-form-item label="原价" prop="oldSalesPrice">
          <el-input v-model="form.oldSalesPrice" placeholder="请输入原价" />
        </el-form-item>
        <el-form-item label="采购价" prop="purchasePrice">
          <el-input v-model="form.purchasePrice" placeholder="请输入采购价" />
        </el-form-item>
        <el-form-item label="上架标志">
          <el-radio-group v-model="form.published">
            <el-radio
              v-for="dict in publishedOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除标志">
          <el-radio-group v-model="form.deleted">
            <el-radio
              v-for="dict in deletedOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="显示顺序" prop="displayOrder">
          <el-input v-model="form.displayOrder" placeholder="请输入显示顺序" />
        </el-form-item>

    </div>
    </el-col>
</el-row>



        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct, exportProduct } from "@/api/catalog/product";
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "Product",
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 上架标志字典
      publishedOptions: [],
      // 删除标志字典
      deletedOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productCode: null,
        alternativeName: null,
        lookup: null,
        categoryId: null,
        specification: null,
        brand: null,
        sku: null,
        vendorId: null,
        published: null,
        createdBy: null,
        createdAt: null,
        updatedBy: null,
        updatedAt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_show_hide").then(response => {
      this.publishedOptions = response.data;
    });
    this.getDicts("sys_show_hide").then(response => {
      this.deletedOptions = response.data;
    });
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 上架标志字典翻译
    publishedFormat(row, column) {
      return this.selectDictLabel(this.publishedOptions, row.published);
    },
    // 删除标志字典翻译
    deletedFormat(row, column) {
      return this.selectDictLabel(this.deletedOptions, row.deleted);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        productName: null,
        productCode: null,
        alternativeName: null,
        lookup: null,
        categoryId: null,
        coverId: null,
        coverUrl: null,
        specification: null,
        purchaseUnit: null,
        standardUnit: null,
        unboxUnit: null,
        brand: null,
        metaKeywords: null,
        metaTitle: null,
        metaDescription: null,
        sku: null,
        shortDescription: null,
        fullDescription: null,
        vendorId: null,
        salesPrice: null,
        oldSalesPrice: null,
        purchasePrice: null,
        published: 0,
        deleted: 0,
        displayOrder: null,
        createdBy: null,
        createdAt: null,
        updatedBy: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    goAddPage () {
      this.$router.push('/catalog/product/add')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productId = row.productId || this.ids
      getProduct(productId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品";
      });
    },
    onCoverUploaded(cover) {
      // console.log('上传封面：', cover.url)
      window.sessionStorage.setItem('picture', JSON.stringify(cover.picture))
      this.form.changeCover = true
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.productId != null) {
            if(this.form.changeCover){
              const cover = window.sessionStorage.getItem('picture')
                if(cover){
                  this.form.cover = JSON.parse(cover)
                  // console.log('修改了封面：', this.form.cover)
                }
            }
            
            updateProduct(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      this.$confirm('是否确认删除商品编号为"' + productIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProduct(productIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProduct(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
